<?php
namespace Admin\Controller;

use Think\PageAdmin;


/**
 * 项目管理--门店管理
 */
class StoreController extends BaseController
{
    /**
     * 初始化
     * @return [type] [description]
     */
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 门店列表
     * @return [type] [description]
     */
    public function index()
    {
        $prefix = C('DB_PREFIX');
        $count = M('store')->count();
        $page = new PageAdmin($count, C('PAGE_NUM'));
        $pages['show'] = $page->show();  //分页输出
        $pages['total'] = $count;  //总数
        $pages['lists'] = M('store a')
            ->order("a.id desc")
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();

        $this->seotitle = '门店列表';
        $this->pages = $pages;
        $this->display();
    }

    /**
     * 添加门店
     */
    public function add()
    {
        $this->seotitle = '添加门店';
        $this->display();
    }

    /**
     * 门店保存
     * @return [type] [description]
     */
    public function save()
    {
        $action = I('get.action', '', 'trim');
        $params = I('post.');

        if ($action == 'add') {// 新增

            if (is_array($params['ads'])) {
                $ads = implode(',', $params['ads']);
            } else {
                $ads = '';
            }

            M('store')->add([
                'store_name' => trim($params['store_name']),
                'manager_name' => $params['manager_name'],
                'mobile' => $params['mobile'],
                'password' => md5($params['password']),
                'telephone' => $params['telephone'],
                'headimg' => $params['headimg'],
                'province' => $params['province'],
                'city' => $params['city'],
                'area' => $params['area'],
                'address' => $params['address'],
                'adids' => $ads,
                'longitude' => $params['longitude'],
                'latitude' => $params['latitude'],
                'remark' => $params['remark'],
                'add_time' => time(),
                'update_time' => time()
            ]);

            $this->success('操作成功', U('index'));

        } elseif ($action == 'edit') {// 编辑
            $id = I('post.id', 0, 'intval');
            M('store')->where(['id' => $id])->save([
                'store_name' => trim($params['store_name']),
                'manager_name' => $params['manager_name'],
                'mobile' => $params['mobile'],
                'password' => md5($params['password']),
                'telephone' => $params['telephone'],
                'headimg' => $params['headimg'],
                'province' => $params['province'],
                'city' => $params['city'],
                'area' => $params['area'],
                'address' => $params['address'],
                'longitude' => $params['longitude'],
                'latitude' => $params['latitude'],
                'remark' => $params['remark'],
                'update_time' => time()
            ]);
            $this->success('操作成功', U('index'));
        }
    }

    /**
     * 门店详情
     */
    public function detail()
    {
        $id = I('get.id', '');
        //$id = 12;
        $info = M('store')->where(['id' => $id])->find();
        if($info['adids']){
            $ads = M('ads')->where('id',['in',$info['adids']])->select();
        }else{
            $ads = [];
        }

        if (!$info) {
            $this->error('门店id有误！');
        }
        $this->assign('info', $info);
        $this->assign('ads', $ads);
        $this->display();
    }
}